package com.lvan.excelhandle.controller;

import com.lvan.excelhandle.bean.ResultMsg;
import com.lvan.excelhandle.service.ExcelService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Lvan
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Resource
    private ExcelService service;

    @PostMapping("import")
    public ResultMsg readExcel(MultipartFile file) {
        try {
            service.getStudent4Excel(file, service);
            return new ResultMsg(200, "导入成功");
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultMsg(201, "导入失败");
        }

    }

    @GetMapping("export")
    public ResultMsg writerExcel(HttpServletResponse response) {
        try {
            service.exportStudentInfo(response);
            return new ResultMsg(200, "导出成功");
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultMsg(201, "导出失败");
        }
    }
}
